package com.xxx.generic;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @version v1.0
 * @Description:
 * @Author zhanzhongxin
 * @Date 2021/7/21 10:51
 * @Copyright 博为峰
 */
public class GenericDemo01 {
    public static void main(String[] args) {
        /*
           泛型:泛指（不具体）的类型，jdk1.5出现的安全机制
           好处：
           1.将运行时ClassCastException转到了编译阶段
           2.避免了强制类型转换

           E Element
           T Type
           K Key
           V Value
         */
//        m1();
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("a");
        arrayList.add("b");
        arrayList.add("c");
//        arrayList.add(1);//new Integer(1) ClassCastException

//        Iterator iterator = arrayList.iterator();
        Iterator<String> iterator = arrayList.iterator();
        while (iterator.hasNext()) {
//            String s = (String) iterator.next();
            String s = iterator.next();
            System.out.println(s);
        }

    }

    public static void m1() {
        ArrayList arrayList = new ArrayList();
        arrayList.add("a");
        arrayList.add("b");
        arrayList.add(1);//new Integer(1) ClassCastException

        Iterator iterator = arrayList.iterator();
        while (iterator.hasNext()) {
            String s = (String) iterator.next();
            System.out.println(s);
        }
    }
}
